package com.hellogood.yaohuo.server.mgr;

import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.shared.entity.Detail;
import com.hellogood.yaohuo.shared.entity.Form;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.entity.Ziyou;
import com.hellogood.yaohuo.shared.entity.ZyForm;
import com.hellogood.yaohuo.shared.exception.DatabaseException;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class ZiyouMgr {
	private String tableFormName = "ZyForm";
	private String billNoField = "ZyBillNo";
	private Logger log = Logger.getLogger(this.getClass());
	
	public List<Ziyou> getAllZiyou() {
		return DAOFactory.getInstance().createZiyouDAO().getAllZiyou();
	}

	public String addNewZiyou(Ziyou z) {
		Ziyou ziyou = DAOFactory.getInstance().createZiyouDAO().getZiyouByCode(z);
		if (ziyou != null) {
			return "编码：" + z.getZiyouCode() + " 已经存在";
		}
		ziyou = DAOFactory.getInstance().createZiyouDAO().getZiyouByName(z);
		if (ziyou != null) {
			return "编码：" + z.getZiyouCode() + " 已经存在";
		}
		return DAOFactory.getInstance().createZiyouDAO().addNewZiyou(z);
	}

	public String ModifyZiyou(Ziyou z) {
		Ziyou ziyou = DAOFactory.getInstance().createZiyouDAO().getZiyouByCode(z);
		ziyou = DAOFactory.getInstance().createZiyouDAO().getZiyouByName(z);
		if (ziyou != null) {
			return "编码：" + z.getZiyouCode() + " 已经存在";
		}
		return DAOFactory.getInstance().createZiyouDAO().ModifyZiyou(z);
	}

	public String getLastCode() {
		return DAOFactory.getInstance().createZiyouDAO().getLastCode();
	}

	public int getLastZyBillNo(String billNo) {
		String no = DAOFactory.getInstance().createZyFormDAO().getLastBillNo(billNo, tableFormName, billNoField);
		if (no != null) {
			// 序号为最后两位 格式为"WLYH组织号YYYYMMDDXX" XX为序号
			if (no.length() > 4) {
				String lastNo = no.substring(no.length() - 4, no.length());
				return Integer.valueOf(lastNo);
			}
		}
		return 0;
	}

	public String createZiyouForm(ZyForm form) {
		String result = DAOFactory.getInstance().createZyFormDAO().createZiyou(form, false);
		if (result.equals("")) {
			addLog(form.getZybillNO(), false);
		}
		return result;
	}

	public String modifyZiyouForm(ZyForm form) {
		String result = DAOFactory.getInstance().createZyFormDAO().createZiyou(form, true);
		if (result.equals("")) {
			addLog(form.getBillNo(), true);
		}
		return result;
	}

	private void addLog(String billNo, boolean isModify) {
		int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
		logID++;
		Log log = new Log();
		log.setLogId(logID);
		log.setStaffCode(MainFrame.currentUser.getStaffCode());
		log.setStaffName(MainFrame.currentUser.getStaffName());
		log.setLogDate(DateUtil.currentTime());
		if (isModify) {
			log.setContent("修改自由补货单：" + billNo);
		} else {
			log.setContent("新建自由补货单：" + billNo);
		}
		DAOFactory.getInstance().createLogDAO().addNewLog(log);
	}

	public List<Form> searchZyForm(String code, int level, String startDate, String endDate, String rz) {
		if (code.equals("0")) {
			return DAOFactory.getInstance().createZyFormDAO().searchFormByQuyu(code, startDate, endDate, rz);
		} else {
			return DAOFactory.getInstance().createZyFormDAO().searchForm(code, startDate, endDate, rz);
		}
	}
	
	public Form getFormDetail(String billNo) {
		Form form = DAOFactory.getInstance().createZyFormDAO().getFormByBillNo(billNo);
		if (form != null) {
			List<Detail> list = DAOFactory.getInstance().createZyDetailDAO().getDetailByBillNo(billNo);
			form.setList(list);
		}
		return form;
	}
	
	public String rz(List<String> bills) {
		String result = "";
		try {
			for (String bill : bills) {
				String temp = DAOFactory.getInstance().createZyFormDAO().rz(bill);
				if (!temp.equals("")) {
					result = temp;
					break;
				}
			}
		} catch (DatabaseException e) {
			log.error(e.getMessage());
			e.printStackTrace();
			result = e.getMessage();
		}
		return result;
	}

}
